1
|
|
|
/** |
2
|
|
|
* Shortcut function that returns the id attribute of an object. |
3
|
|
|
* @param item |
4
|
|
|
*/ |
5
|
|
|
export function getId<T extends { id: number }>(item: T): number { |
6
|
|
|
return item.id; |
7
|
|
|
} |
8
|
|
|
|
9
|
|
|
export function identity<T>(value: T): T { |
10
|
|
|
return value; |
11
|
|
|
} |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Returns true if value is not null or undefined. |
15
|
|
|
* Can be used to filter nulls and undefined values out of an array. |
16
|
|
|
* @param item |
17
|
|
|
*/ |
18
|
|
|
export function notEmpty<T>(value: T | null | undefined): value is T { |
19
|
|
|
return value !== null && value !== undefined; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
export function stringNotEmpty(value: string | null): value is string { |
23
|
|
|
return notEmpty(value) && value.length > 0; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Returns true if value id null OR undefined. |
28
|
|
|
* @param item |
29
|
|
|
*/ |
30
|
|
|
export function empty<T>( |
31
|
|
|
value: T | null | undefined, |
32
|
|
|
): value is null | undefined { |
33
|
|
|
return value === null || value === undefined; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* From an array of objects, return the first object with a specific id value. |
38
|
|
|
* @param objs |
39
|
|
|
* @param id |
40
|
|
|
*/ |
41
|
|
|
export function find<T extends { id: number }>( |
42
|
|
|
objs: T[], |
43
|
|
|
id: number, |
44
|
|
|
): T | null { |
45
|
|
|
const found = objs.filter((item) => item.id === id); |
46
|
|
|
return found.length > 0 ? found[0] : null; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Return all objects from array with the specified key-value attribute. |
51
|
|
|
* @param objs |
52
|
|
|
* @param prop |
53
|
|
|
* @param value |
54
|
|
|
*/ |
55
|
|
|
export function where<T, K extends keyof T>( |
56
|
|
|
objs: T[], |
57
|
|
|
prop: K, |
58
|
|
|
value: any, |
59
|
|
|
): T[] { |
60
|
|
|
return objs.filter((obj) => prop in obj && obj[prop] === value); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Return first object from array with the specified key-value attribute. |
65
|
|
|
* @param objs |
66
|
|
|
* @param prop |
67
|
|
|
* @param value |
68
|
|
|
*/ |
69
|
|
|
export function whereFirst<T, K extends keyof T>( |
70
|
|
|
objs: T[], |
71
|
|
|
prop: K, |
72
|
|
|
value: any, |
73
|
|
|
): T { |
74
|
|
|
return where(objs, prop, value)[0]; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Map each key-value attribute of an object into an array |
79
|
|
|
* @param object |
80
|
|
|
* @param mapFn |
81
|
|
|
*/ |
82
|
|
|
export function objectMap<A, B>( |
83
|
|
|
object: { |
84
|
|
|
[key: string]: A; |
85
|
|
|
}, |
86
|
|
|
mapFn: (key: string, value: A) => B, |
87
|
|
|
): B[] { |
88
|
|
|
return Object.keys(object).reduce((result: B[], key: string): B[] => { |
89
|
|
|
result.push(mapFn(key, object[key])); |
90
|
|
|
return result; |
91
|
|
|
}, []); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Return a copy of an object, with each value being passed through a map function. |
96
|
|
|
* Each value's key is unaffected. |
97
|
|
|
* @param object |
98
|
|
|
* @param mapFn |
99
|
|
|
*/ |
100
|
|
|
export function mapObjectValues<A, B>( |
101
|
|
|
object: IndexedObject<A>, |
102
|
|
|
mapFn: (value: A) => B, |
103
|
|
|
): IndexedObject<B> { |
104
|
|
|
return Object.entries(object).reduce( |
105
|
|
|
(result: IndexedObject<B>, [key, value]) => { |
106
|
|
|
result[key] = mapFn(value); |
107
|
|
|
return result; |
108
|
|
|
}, |
109
|
|
|
{}, |
110
|
|
|
); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
type IndexedObject<T> = { |
114
|
|
|
[key: string]: T; |
115
|
|
|
}; |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Maps an array of items into an object, with each transformed into an attribute |
119
|
|
|
* @param items array of objects |
120
|
|
|
* @param keyFn Function that returns a unique key for each object. Typically the getId function. |
121
|
|
|
* @param valFn Function that returns a new value for each object. |
122
|
|
|
*/ |
123
|
|
|
export function mapToObjectTrans<A, B>( |
124
|
|
|
items: A[], |
125
|
|
|
keyFn: (item: A) => string | number, |
126
|
|
|
valFn: (item: A) => B, |
127
|
|
|
): IndexedObject<B> { |
128
|
|
|
return items.reduce((result: IndexedObject<B>, item: A): IndexedObject<B> => { |
129
|
|
|
const key = keyFn(item); |
130
|
|
|
result[key] = valFn(item); |
131
|
|
|
return result; |
132
|
|
|
}, {}); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Maps an array of items into an object, with each item set as an attribute. |
137
|
|
|
* @param items array of objects |
138
|
|
|
* @param keyFn Function that returns a unique key for each object. Typically the getId function. |
139
|
|
|
*/ |
140
|
|
|
export function mapToObject<T>( |
141
|
|
|
items: T[], |
142
|
|
|
keyFn: (item: T) => string | number, |
143
|
|
|
): IndexedObject<T> { |
144
|
|
|
return mapToObjectTrans(items, keyFn, (item): T => item); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Checks if an object has an attribute with a particular key |
149
|
|
|
* @param object |
150
|
|
|
* @param key |
151
|
|
|
*/ |
152
|
|
|
export function hasKey<T>( |
153
|
|
|
object: { [key: string]: T }, |
154
|
|
|
key: string | number, |
155
|
|
|
): boolean { |
156
|
|
|
return object[key] !== undefined; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Returns the value at the specified key. If the key is not present, throws an error. |
161
|
|
|
* @param object |
162
|
|
|
* @param key |
163
|
|
|
* @param errorMessage |
164
|
|
|
*/ |
165
|
|
|
export function getOrThrowError<T>( |
166
|
|
|
object: { [key: string]: T }, |
167
|
|
|
key: string | number, |
168
|
|
|
errorMessage: string, |
169
|
|
|
): T { |
170
|
|
|
if (!hasKey(object, key)) { |
171
|
|
|
throw new Error(errorMessage); |
172
|
|
|
} |
173
|
|
|
return object[key]; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** Return a copy of the object with specific property removed */ |
177
|
|
|
export function deleteProperty<T, K extends keyof T>( |
178
|
|
|
obj: T, |
179
|
|
|
key: K, |
180
|
|
|
): Omit<T, K> { |
181
|
|
|
const { [key]: _, ...newObj } = obj; |
182
|
|
|
return newObj; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Iterate through the properties of the object, checking which return true for the filterFunction. |
187
|
|
|
* Return a copy of the object, without the properties that don't pass the filter. |
188
|
|
|
*/ |
189
|
|
|
export function filterObjectProps<T>( |
190
|
|
|
obj: IndexedObject<T>, |
191
|
|
|
filter: (value: T) => boolean, |
192
|
|
|
): IndexedObject<T> { |
193
|
|
|
return Object.entries(obj).reduce( |
194
|
|
|
(newObj: IndexedObject<T>, [key, value]): IndexedObject<T> => { |
195
|
|
|
if (filter(value)) { |
196
|
|
|
newObj[key] = value; |
197
|
|
|
} |
198
|
|
|
return newObj; |
199
|
|
|
}, |
200
|
|
|
{}, |
201
|
|
|
); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** Return a copy of the list with duplicate elements removed */ |
205
|
|
|
export function uniq<T>(x: T[]): T[] { |
206
|
|
|
return Array.from(new Set(x)); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
export function flatten<T>(x: T[][]): T[] { |
210
|
|
|
const concat = (ls: T[], xs: T[]): T[] => ls.concat(xs); |
211
|
|
|
return x.reduce(concat, []); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
export function removeDuplicatesById<T extends { id: number }>( |
215
|
|
|
items: T[], |
216
|
|
|
): T[] { |
217
|
|
|
interface Accumulator { |
218
|
|
|
contents: T[]; |
219
|
|
|
ids: number[]; |
220
|
|
|
} |
221
|
|
|
const reducer = (result: Accumulator, next: T): Accumulator => { |
222
|
|
|
if (!result.ids.includes(next.id)) { |
223
|
|
|
result.contents.push(next); |
224
|
|
|
result.ids.push(next.id); |
225
|
|
|
} |
226
|
|
|
return result; |
227
|
|
|
}; |
228
|
|
|
return items.reduce(reducer, { contents: [], ids: [] }).contents; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Decrement the number if it above zero, else return 0. |
233
|
|
|
* This helps to avoid some pathological edge cases where pendingCount becomes permanently bugged. |
234
|
|
|
* @param num |
235
|
|
|
*/ |
236
|
|
|
export function decrement(num: number): number { |
237
|
|
|
return num <= 0 ? 0 : num - 1; |
238
|
|
|
} |
239
|
|
|
|